<script setup>
import { getPageTradeOrderApi, refreshTradeOrderApi } from "@/api/tradeOrder";
import $bus from "../../utils/bus";
import { ElMessage } from "element-plus";
import { onMounted, ref } from "vue";
// ---------------------------------------------------------------------获取查询刷新---------------------------------------------------------------------
const dataList = ref([]);
const queryPageParams = ref({
  pageNum: 1,
  pageSize: 10,
  orderCode: "",
  selectType: "",
  dateTime: [
    new Date().toISOString().split("T")[0], // 今天
    new Date().toISOString().split("T")[0], // 今天
  ],
});
// 订单状态 1：待分拣 2：分拣中 3：配送中 4：已签收 11：分拣完成
const selectTypeOptions = [
  {
    value: 1,
    label: "待分拣",
  },
  {
    value: 2,
    label: "分拣中",
  },
  {
    value: 3,
    label: "配送中",
  },
  {
    value: 4,
    label: "已签收",
  },
  {
    value: 11,
    label: "分拣完成",
  },
];
const total = ref(0);
const getData = async () => {
  console.log(queryPageParams.value);
  const res = await getPageTradeOrderApi(queryPageParams.value);
  dataList.value = res.data.list;
  total.value = res.data.total;
};
const getPage = () => {
  getData();
};
const clear = () => {
  queryPageParams.value.pageNum = 1;
  queryPageParams.value.pageSize = 10;
  (queryPageParams.value.orderCode = ""),
    (queryPageParams.value.selectType = ""),
    (queryPageParams.value.dateTime = [
      new Date().toISOString().split("T")[0], // 今天
      new Date().toISOString().split("T")[0], // 今天
    ]);
  getData();
};

//---------------------------------------------------------------------刷新数据事件---------------------------------------------------------------------
const open = ref(false);
const form = ref({
  dateTime: [
    new Date().toISOString().split("T")[0], // 今天
    new Date().toISOString().split("T")[0], // 今天
  ],
});
const formRef = ref(null);
const refresh = () => {
  open.value = true;
};
const closeDialog = () => {
  open.value = false;
  form.value.dateTime = [
    new Date().toISOString().split("T")[0], // 今天
    new Date().toISOString().split("T")[0], // 今天
  ];
};

const submit = async () => {
  const res = await refreshTradeOrderApi(form.value);
  ElMessage({
    message: res.data,
    type: "success",
  });
  getData();
  closeDialog();
};
const handlePageChange = () => {
  getData();
};
//---------------------------------------------------------------------获取标题事件---------------------------------------------------------------------
const titleData = ref({
  titleFir: "贸易数据",
  titleSec: "订单信息",
});
onMounted(() => {
  $bus.emit("title-info", titleData.value);
  getData();
});
</script>
<template>
  <el-card>
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="订单号">
        <el-input placeholder="请输入订单号" v-model="queryPageParams.orderCode" />
      </el-form-item>
      <el-form-item label="订单状态">
        <el-select
          v-model="queryPageParams.selectType"
          placeholder="请选择订单状态"
          style="width: 200px"
        >
          <el-option
            v-for="item in selectTypeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="收货时间">
        <el-date-picker
          v-model="queryPageParams.dateTime"
          type="daterange"
          range-separator="To"
          start-placeholder="Start date"
          end-placeholder="End date"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="Search" type="primary" @click="getPage()">查询</el-button>
        <el-button icon="RefreshRight" type="info" @click="clear()">重置</el-button>
        <el-button icon="Edit" type="danger" @click="refresh()">刷新</el-button>
      </el-form-item>
    </el-form>
  </el-card>

  <el-card>
    <el-table
      :data="dataList"
      stripe
      fit
      default-expand-all
      row-key="id"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      scrollbar-tabindex
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column width="55" type="index" label="序号"></el-table-column>
      <el-table-column width="150" label="订单号" prop="orderSerialNo"></el-table-column>
      <el-table-column width="120" label="收货时间" prop="orderReceiveTime"></el-table-column>
      <el-table-column width="120" label="下单时间" prop="orderTime"></el-table-column>
      <el-table-column width="230" label="收货人名称" prop="orderReceiveCustomerName"></el-table-column>
      <el-table-column width="110" label="下单金额" prop="orderPrice"></el-table-column>
      <!-- 订单状态 1：待分拣 2：分拣中 3：配送中 4：已签收 11：分拣完成 -->
      <el-table-column width="80" label="订单状态" prop="orderState">
        <template #default="data">
          <!-- <span>{{ data.row }}</span> -->
          <el-tag v-if="data.row.orderState === 0" type="info">无</el-tag>
          <el-tag v-else-if="data.row.orderState === 1" type="danger">待分拣</el-tag>
          <el-tag v-else-if="data.row.orderState === 2" type="warning">分拣中</el-tag>
          <el-tag v-else-if="data.row.orderState === 3" type="info">配送中</el-tag>
          <el-tag v-else-if="data.row.orderState === 4" type="primary">已签收</el-tag>
          <el-tag v-else-if="data.row.orderState === 11" type="primary">分拣完成</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column width="150" label="订单状态" prop="orderPayState"></el-table-column> -->
      <!-- 1,订单支付状态 0：无特殊含义 1：未支付 2：部分支付 3：支付完成 4：已退款 5：已关闭 6：超额支付 -->
      <el-table-column width="80" label="支付状态" prop="orderPayState">
        <template #default="data">
          <!-- <span>{{ data.row }}</span> -->
          <el-tag v-if="data.row.orderPayState === 0" type="info">无</el-tag>
          <el-tag v-else-if="data.row.orderPayState === 1" type="danger">未支付</el-tag>
          <el-tag v-else-if="data.row.orderPayState === 2" type="warning">部分支付</el-tag>
          <el-tag v-else-if="data.row.orderPayState === 3" type="primary">支付完成</el-tag>
          <el-tag v-else-if="data.row.orderPayState === 4" type="info">已退款</el-tag>
          <el-tag v-else-if="data.row.orderPayState === 5" type="info">已关闭</el-tag>
          <el-tag v-else-if="data.row.orderPayState === 6" type="info">超额支付</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column width="150" label="出库状态" prop="orderOutstockState"></el-table-column> -->
      <!-- '出库状态，全部出库才是已出库 0：无 1：待出库 2：部分出库 3：已出库 -->
      <el-table-column width="80" label="出库状态" prop="orderOutstockState">
        <template #default="data">
          <!-- <span>{{ data.row }}</span> -->
          <el-tag v-if="data.row.orderOutstockState === 0" type="info">无</el-tag>
          <el-tag v-else-if="data.row.orderOutstockState === 1" type="danger">待出库</el-tag>
          <el-tag v-else-if="data.row.orderOutstockState === 2" type="warning">部分出库</el-tag>
          <el-tag v-else-if="data.row.orderOutstockState === 3" type="primary">已出库</el-tag>
        </template>
      </el-table-column>

      <el-table-column width="110" label="出库金额" prop="orderOutstockPrice"></el-table-column>
      <el-table-column width="110" label="加单总金额" prop="orderTotalAddOrderPrice"></el-table-column>
      <el-table-column width="115" label="未称重任务数" prop="orderSortingUnweightCount"></el-table-column>
      <el-table-column width="100" label="缺货任务数" prop="orderSortingOutStockCount"></el-table-column>
      <el-table-column width="100" label="订单商品数" prop="orderDetailLength"></el-table-column>
      <el-table-column width="125" label="订单组合商品数" prop="orderRawDetailLength"></el-table-column>
      <el-table-column width="100" label="总加单数" prop="orderTotalAddOrderValueQuantity"></el-table-column>
      <el-table-column width="115" label="下单金额补差" prop="orderExtraOrderPrice"></el-table-column>
      <el-table-column width="100" label="售后金额" prop="orderAftersalePrice"></el-table-column>

    </el-table>

    <el-pagination
      v-model:current-page="queryPageParams.pageNum"
      v-model:page-size="queryPageParams.pageSize"
      :page-sizes="[10, 50, 100, 200]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="+total"
      @change="handlePageChange"
    />
  </el-card>

  <el-dialog v-model="open" title="刷新" width="50%" :before-close="closeDialog" draggable>
    <el-form :model="form" label-width="auto" ref="formRef">
      <el-form-item label="刷新时间">
        <el-date-picker
          v-model="form.dateTime"
          type="daterange"
          range-separator="To"
          start-placeholder="Start date"
          end-placeholder="End date"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="closeDialog()">取消</el-button>
        <el-button type="primary" @click="submit()"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss"></style>
